﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;
using NuiDicomPlayer.Nui.Gestures;
using Biomedica.Graphics;
namespace NuiDicomPlayer.Nui.NavigationEngine
{
    /// <summary>
    /// This class will detect if the skeleton is in the Viewing Stage
    /// </summary>
    public class SkeletonPostionDetector
    {

        public Boolean Instage { set; get; }

        Skeleton[] BufferdSkeletons { set; get; }

        public double StageMAXLimit { set; get; }
        public double StageMINLimit { set; get; }

        /// <summary>
        /// Constract an in Stage Detector
        /// </summary>
        public SkeletonPostionDetector()
        {
            StageMAXLimit=2.5;
            StageMINLimit = 1.5;
            
        }
        /// <summary>
        /// Detect if the Skeleton Is in Stage
        /// </summary>
        /// <returns>true is in stage</returns>
        public bool IsSkeletonInstage(Skeleton[] BufferdSkeletons)
        {
          
               
                   this.BufferdSkeletons = BufferdSkeletons;
                    double ma = (FindMA()).Z;

                    Instage = (ma > StageMINLimit) &(ma <= StageMAXLimit);
                    //System.Diagnostics.Trace.WriteLine("Distance from COG to Camera is = " + ma.ToString() + "   ,   " + StageMINLimit.ToString());
                    BufferdSkeletons = null;
         

            return Instage;
        }

        /// <summary>
        /// Find MA
        /// </summary>
        /// <returns></returns>
        private Vector3 FindMA()
        {
            Vector3 maV = Vector3.One;
            double ma = 0;
            foreach (Skeleton s in BufferdSkeletons)
            {

                /// Calculate the MA of center of gravity using Vecdtor3
                /// Ignore any error
                if(s!=null)
                maV += new Vector3(s.Position.X, s.Position.Y, s.Position.Z);


            }

            return maV / BufferdSkeletons.Length;
        }
    }
}
